<%--
  Created by IntelliJ IDEA.
  User: HAN
  Date: 2020/11/22
  Time: 16:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>
<div id="addGustForm">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">设施名</label>
            <div class="layui-input-block">
                <input type="text"  name="name" required  lay-verify="required" placeholder="请输入设施名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="text"  name="count" required  lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">设施类型ID</label>
            <div class="layui-input-block">
                <%--<input type="text" name="typeid" required   placeholder="请输入设施类型ID" autocomplete="off" class="layui-input">--%>
                    <select name="typeid" class="selec" lay-verify="">
                    </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">设施价钱</label>
            <div class="layui-input-block">
                <input type="text" name="pmoney" required   placeholder="请输入设施价钱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-block">
                <input type="text" name="gys" required   placeholder="请输入供应商" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" >立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<%--form表单 用于修改数据--%>
<div id="updGustForm">
    <form class="layui-form" action="" lay-filter="modify">
        <div class="layui-form-item">
            <label class="layui-form-label">id</label>
            <div class="layui-input-block">
                <input type="text"  name="id" required  lay-verify="required" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">设施名</label>
            <div class="layui-input-block">
                <input type="text"  name="name" required  lay-verify="required" placeholder="请输入设施名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="text"  name="count" required  lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">设施类型ID</label>
            <div class="layui-input-block">
                <%--<input type="text" name="typeid" required   placeholder="请输入设施类型ID" autocomplete="off" class="layui-input">--%>
                <select name="typeid" class="selec" lay-verify="">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">设施价钱</label>
            <div class="layui-input-block">
                <input type="text" name="pmoney" required   placeholder="请输入设施价钱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-block">
                <input type="text" name="gys" required   placeholder="请输入供应商" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" >立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    {{# if(d.count>0) { }}
    <a class="layui-btn layui-btn-xs layui-btn-disabled " >删除</a>
    {{# }else{    }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
</script>

<form class="layui-form" action="" style="padding-top:10px ">
    <div class="layui-inline">
        <label class="layui-form-label">设施编号</label>
        <div class="layui-input-inline">
            <input type="text" name="id" placeholder="请输入用户编号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">设施名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" placeholder="请输入用户名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo1">查询</button>
        </div>
    </div>
</form>


<table class="layui-hide" id="test" lay-filter="test"></table>
<script>
    layui.use(['table','layer','form','jquery'], function(){
        var table = layui.table;
        var layer=layui.layer;
        var $=layui.jquery;
        var form=layui.form;
        //隐藏form表单
        $("#addGustForm").hide();
        $("#updGustForm").hide();

        $.ajax({
            url:"/tstpid"
            ,type:"post"
            ,dataType:"JSON"
            ,success:function (r) {
                $(".selec").empty();
                $.each(r,function (index,item) {
                    $(".selec").append("<option value='"+item.id+"'>"+item.pname+"</option>");
                })
                form.render("select");
            },error:function (r) {
                alert(r)
                alert("错误");
            }
        });

        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/queryALL'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: []
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'设施名',  edit: 'text'}
                ,{field:'count', title:'数量',  edit: 'text'}
                ,{field:'pmoney', title:'设施价钱',  edit: 'text'}
                ,{field:'gys', title:'供应商',  edit: 'text'}
                ,{field:'pname', title:'设施类型',  edit: 'text'}

                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
            ,limit:5
            ,limits:[5,10,15,20,25]

        });


        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;

            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //obj.del();
                    /*window.location.href="/myConterall?del";
                    layer.close(index);*/
                    $.ajax({
                        url:"/tstpdel"
                        ,type:"post"
                        ,data:{id:data.id}
                        ,success:function (r) {
                            layer.msg("删除成功！！")
                        },error:function(msg) {
                            alert("服务器错误");
                        }
                    })
                    layer.close(index);
                    table.reload('test',{
                        page:{
                            curr:1
                        }
                    })

                });
            } else if(obj.event === 'edit'){
                //获得dian'j
                form.val("modify",(data));
                layer.open({
                    title: '修改信息'
                    ,type:1
                    ,area: ['600px', '450px']
                    ,offset: '50px'
                    ,content:$("#updGustForm")
                });



                form.on('submit(formDemo)', function(data){
                    //layer.msg(JSON.stringify(data.field));
                    //1.将获取到到的数据保存到数据库
                    $.ajax({
                        url:"/tstpadd",
                        data:data.field,  //当前表单里面填写的数据{name:'',name1:''}
                        type:"post",
                        success:function(r){
                            layer.msg("修改成功！！")
                            table.reload('test',{
                                page:{
                                    curr:1
                                }
                            })

                        },error:function(msg){
                            alert("服务器错误")
                        }

                    })
                    //刷新一下table
                    table.reload('test',{
                        page:{
                            curr:1
                        }
                    })
                    layer.closeAll();

                    return false;
                });
            }

        });

        table.on('toolbar(test)',function (obj) {
            layer.open({
                title:'添加用户'
                ,type:1
                ,area:['500px','350']
                ,offset:'50px'
                ,content:$("#addGustForm")
            });


            form.on('submit(formDemo)', function(data){
                //layer.msg(JSON.stringify(data.field));
                //1.将获取到到的数据保存到数据库
                $.ajax({
                    url:"/tstpadd",
                    data:data.field,  //当前表单里面填写的数据{name:'',name1:''}
                    type:"post",
                    success:function(r){
                        layer.closeAll();
                        layer.msg("添加成功！！")
                    },error:function(msg){
                        alert("服务器错误");
                    }
                })

                //刷新一下table
                table.reload('test',{
                    page:{
                        curr:1
                    }
                })
                return false;
            });

        });


        form.on('submit(formDemo1)', function(data){
            table.reload("test",{
                page:{
                    //从第一页开始加载
                    curr:1
                },where:data.field
            })
            return false;
        });

    });
</script>
</body>
</html>
